package com.example.Annocation.profile;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @Description TODO
 * @Date 2024/5/15 21:47
 * @Created by houjunfeng
 */
@Configuration
//3.@Profile("test") 写在类上时只有运行在此环境下配置类才会加载
@Profile("dev")
@PropertySource(value = "classpath:/spring.properties")
public class ProfileConfiguration {
    @Value("${datasource.username}")
    private String user;
    @Value("${datasource.password}")
    private String password;
    @Value("${datasource.driver-class-name}")
    private String driverClass;
    //@Profile("default")默认环境，启动时默认为这个


    @Profile("test")
    @Bean(name = "dataSourceTest")
    public DataSource getDataSourceTest() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("${datasource.urlTest}");
        dataSource.setDriverClass(driverClass);
        return  dataSource;
    }


    @Profile("dev")
    @Bean(name = "dataSourceDev")
    public DataSource getDataSourceDev() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("${datasource.urlDev}");
        dataSource.setDriverClass(driverClass);
        return  dataSource;
    }


    @Profile("pro")
    @Bean(name = "dataSourcePro")
    public DataSource getDataSourcePro() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("${datasource.urlPro}");
        dataSource.setDriverClass(driverClass);
        return  dataSource;
    }

//    @Bean
//    public JdbcTemplate getJdbcTemplate() throws Exception {
//        //Spring对@Configuration做特殊处理，这里调用getDataSourceTest()指把组件导入进来
//         JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceTest());
//         return  jdbcTemplate;
//    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
